﻿using EasyTest.Shared.Extensions;
using EasyTest.Shared.TestAttribute;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;


namespace ZLGCANoptimize
{
    public class WIFI
    {
        [ExposedMethodAttribute("")]
        public bool 打开WIFI4C30(int 通道, out string 信息, out string data)
        {
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    return false;
                }
                /*
                   10 08 2E 4C 30 00 00 00
                    21 00 02 00 00 00 00 00
                */
                bool result1 = false, result2 = false;//05 2E 4C 83 01 00 00 00
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, "10 08 2E 4C 30 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(80);
                    result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 02 00 00 00 00 00", true, SendType.正常发送);
                    if (result1 && result2)
                    {
                        break;
                    }
                }

                if (!result1 || !result2)
                {
                    data = "发送数据失败,打开WIFI失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 30"))
                        {
                            信息 = "打开WIFI成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "打开WIFI失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "WIFI失败" + ex.Message;
                return false;
            }
        }
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;
        [ExposedMethodAttribute("")]
        public bool 读取WIFIMAC地址4C33(int 通道, out string mac地址, out int mac地址长度, out string 信息, out string data)
        {
            data = "";
            mac地址 = "";
            mac地址长度 = 0;
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1, result2;//03 22 4C 33 00 00 00 00
                result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C 33 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(50);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(50);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(100);
                if (!result1)
                {
                    信息 = "发送数据失败,读取Wifi Mac地址失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join(",", datas);
                string[] hexValues = new string[18];
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                int num = 0;
                /*  10 15 62 4C 33 00 30 30
                    21 3A 30 38 3A 32 32 3A
                    22 41 34 3A 39 41 3A 46
                    23 42 AA AA AA AA AA AA
                 */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i <= 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("23"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 1) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }

                mac地址 = string.Join("", hexValues);

                mac地址长度 = mac地址.Length;
                信息 = string.Join(",", hexValues);
                if (ok10 && mac地址.Length == 34)
                {

                    var hexValues0 = "";
                    foreach (String hex in hexValues)
                    {
                        int value = Convert.ToInt32(hex, 16);
                        string stringValue = Char.ConvertFromUtf32(value);
                        hexValues0 += stringValue;
                    }
                    信息 = string.Join(",", datas);
                    mac地址 = hexValues0;

                    return true;
                }
                else if (mac地址.Length != 18)
                {
                    信息 = "长度NG";

                }
                else
                {
                    return false;
                }

                //3、关闭
                信息 = "读取Wifi Mac地址失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "读取Wifi Mac地址失败" + ex.Message;
                return false;
            }
        }

//        [ExposedMethodAttribute("")]
//        public bool 打开WIFI(int 通道, out string 信息, out string data)
//        {
//#if ELO20240927
//            data = "";
//            try
//            {
//                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
//                {
//                    信息 = "启动can设备失败";
//                    return false;
//                }
//                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
//                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
//                if (!OK)
//                {
//                    return false;
//                }
//                /*
//                   10 08 2E 4C 30 00 00 00
//                */
//                bool result1 = false;//05 2E 4C 83 01 00 00 00
//                for (int i = 0; i < 3; i++)
//                {
//                    result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 30 02 00 00 00", true, SendType.正常发送);
//                    if (result1)
//                    {
//                        break;
//                    }
//                }

//                if (!result1)
//                {
//                    data = "发送数据失败,打开WIFI失败";
//                    return false;
//                }
//                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
//                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
//                data = string.Join("\n", datas);
//                if (datas.Length > 0)
//                {
//                    foreach (string s in datas)
//                    {

//                        if (s.Trim().StartsWith("03 6E 4C 30"))
//                        {
//                            信息 = "打开WIFI成功";
//                            return true;
//                        }
//                    }

//                }

//                信息 = "打开WIFI失败";
//            }
//            catch (Exception ex)
//            {
//                信息 = "打开WIFI失败" + ex.Message;
//            }
//            return false;
//#else
//            data = "";
//            try
//            {
//                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
//                {
//                    信息 = "启动can设备失败";
//                    return false;
//                }
//                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
//                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
//                if (!OK)
//                {
//                    return false;
//                }
//                /*
//                   10 08 2E 4C 30 00 00 00
//                   21 00 02 00 00 00 00 00
//                */
//                bool result1 = false, result2 = false;//05 2E 4C 83 01 00 00 00
//                for (int i = 0; i < 3; i++)
//                {
//                    result1 = zLGCANHelper.SentData(通道, 0x761, "10 08 2E 4C 30 00 00 00", true, SendType.正常发送);
//                    Thread.Sleep(80);
//                    result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 02 00 00 00 00 00", true, SendType.正常发送);
//                    if (result1 && result2)
//                    {
//                        break;
//                    }
//                }

//                if (!result1 || !result2)
//                {
//                    data = "发送数据失败,打开WIFI失败";
//                    return false;
//                }
//                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
//                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
//                data = string.Join("\n", datas);
//                if (datas.Length > 0)
//                {
//                    foreach (string s in datas)
//                    {

//                        if (s.Trim().StartsWith("03 6E 4C 30"))
//                        {
//                            信息 = "打开WIFI成功";
//                            return true;
//                        }
//                    }

//                }
//                //3、关闭
//                信息 = "打开WIFI失败";
//                return false;
//            }
//            catch (Exception ex)
//            {
//                信息 = "打开WIFI失败" + ex.Message;
//                return false;
//            }
//#endif
//        }
        [ExposedMethodAttribute("")]
        public bool 关闭WIFI4C30(int 通道, out string 信息, out string data)
        {
#if ELO20240927
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    return false;
                }
                /*
                   10 08 2E 4C 30 00 00 00
                   21 00 02 00 00 00 00 00
                */
                bool result1 = false, result2 = false;//05 2E 4C 83 01 00 00 00
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, "10 08 2E 4C 30 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(80);
                    result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 01 00 00 00 00 00", true, SendType.正常发送);
                    if (result1 && result2)
                    {
                        break;
                    }
                }

                if (!result1 || !result2)
                {
                    data = "发送数据失败,关闭WIFI失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 30"))
                        {
                            信息 = "关闭WIFI成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "关闭WIFI失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "关闭WIFI失败" + ex.Message;
                return false;
            }
#else
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    return false;
                }
                /*
                   10 08 2E 4C 30 00 00 00
                   21 00 02 00 00 00 00 00
                */
                bool result1 = false, result2 = false;//05 2E 4C 83 01 00 00 00
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, "10 08 2E 4C 30 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(80);
                    result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 01 00 00 00 00 00", true, SendType.正常发送);
                    if (result1 && result2)
                    {
                        break;
                    }
                }

                if (!result1 || !result2)
                {
                    data = "发送数据失败,关闭WIFI失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 30"))
                        {
                            信息 = "关闭WIFI成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "关闭WIFI失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "关闭WIFI失败" + ex.Message;
                return false;
            }
#endif
        }

        [ExposedMethodAttribute("")]
        public bool 读连接WIFI信号强度(int 通道, out string 信息, out string data, out int 信号强度)
        {
            data = "";
            信号强度 = -99;
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    return false;
                }
                bool result1 = false;
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C 34 00 00 00 00 00", true, SendType.正常发送);
                    if (result1)
                    {
                        break;
                    }
                }

                if (!result1)
                {
                    data = "发送数据失败,读连接WIFI信号强度失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {

                        if (s.Trim().Contains("62 4C 34"))
                        {
                            信息 = "读连接WIFI信号强度成功";

                            //2024-11-19之前产品一直都是这样解析
                            // 信号强度=  s.Split(' ')[5].ToInt();
                            //信号强度 = Convert.ToInt32(s.Split(' ')[6], 16) * -1; // 转换为十进制整数
                            //if (s.Split(' ')[4] != "00")
                            //{
                            //    信号强度 = -999;
                            //    return false;
                            //}

                            //2024-11-19产品测试出来的情况，需要这样解析才行
                            信号强度 = Convert.ToInt32(s.Split(' ')[5], 16) * -1; // 转换为十进制整数
                            if (s.Split(' ')[5] == "20")
                            {
                                信号强度 = -999;
                                return false;
                            }
                            return true;
                        }
                    }

                }
                //3、关闭
                信息 = "读连接WIFI信号强度失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "读连接WIFI信号强度失败" + ex.Message;
                return false;
            }
        }

        [ExposedMethodAttribute("")]
        public bool 连接WIFI(int 通道, out string 响应, out string 命令, out string 信息, string 账号 = "mFi_160868",
            string 密码 = "1234567890")
        {
#if ELO20240927
            List<string> list = new List<string>()
            { "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
            };

            list.InsertRange(0, 账号.ToHexArray());
            list.InsertRange(账号.Length + 1, 密码.ToHexArray());
            信息 = "";
            响应 = "";
            List<string> list1 = new List<string>();
            string str10 = $"10 23 2E 4C 32 " + String.Join(" ", list.GetRange(0, 3)).Trim();
            string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
            string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
            string str23 = $"23 " + String.Join(" ", list.GetRange(17, 7)).Trim();
            string str24 = $"24 " + String.Join(" ", list.GetRange(24, 7)).Trim();
            string str25 = $"25 " + String.Join(" ", list.GetRange(31, 7)).Trim();
            list1.Add(str10);
            list1.Add(str21);
            list1.Add(str22);
            list1.Add(str23);
            list1.Add(str24);
            list1.Add(str25);
            命令 = string.Join("\n", list1);
            /*
                10 23 2E 4C 32 33 36 30
                21 57 69 46 69 00 77 65
                22 6E 79 6F 6E 67 31 32
                23 33 00 20 20 20 20 20
                24 20 20 20 20 20 20 20
                25 20 00 00 00 00 00 00
             */
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1;

                foreach (var item in list1)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
                    if (!result1)
                    {
                        信息 = "发送数据失败:" + item;
                        return false;
                    }
                    Thread.Sleep(50);
                }

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                响应 = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 32"))
                        {
                            信息 = "连接WIFI成功";
                            return true;
                        }
                    }
                }

                信息 = "连接WIFI失败";
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
            }
            return false;
#else
            List<string> list = new List<string>()
                { "20", "20", "20","20", "20", "20","20", "20", "20",
                    "20", "20", "20","20", "20", "20","20", "20", "20",
                    "20", "20", "20","20", "20", "20","20", "20", "20",
                    "20", "20", "20","20", "20", "20","20", "20", "20",
                };

            list.InsertRange(0, 账号.ToHexArray());//10 13 2E F1 8C 20 32  33 
            list.InsertRange(账号.Length + 1, 密码.ToHexArray());//10 13 2E F1 8C 20 32  33 
            信息 = "";
            响应 = "";
            List<string> list1 = new List<string>();
            string str10 = $"10 23 2E 4C 32 " + String.Join(" ", list.GetRange(0, 3)).Trim();
            string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
            string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
            string str23 = $"23 " + String.Join(" ", list.GetRange(17, 7)).Trim();
            string str24 = $"24 " + String.Join(" ", list.GetRange(24, 7)).Trim();
            string str25 = $"25 " + String.Join(" ", list.GetRange(31, 7)).Trim();
            list1.Add(str10);
            list1.Add(str21);
            list1.Add(str22);
            list1.Add(str23);
            list1.Add(str24);
            list1.Add(str25);
            命令 = string.Join("\n", list1);
            /*
                10 23 2E 4C 32 33 36 30
                21 57 69 46 69 00 77 65
                22 6E 79 6F 6E 67 31 32
                23 33 00 20 20 20 20 20
                24 20 20 20 20 20 20 20
                25 20 00 00 00 00 00 00
             */
            //   命令 = $"06 2E F1 8B {DateTime.UtcNow.ToString("yy")} {DateTime.UtcNow.ToString("MM")} {DateTime.UtcNow.ToString("dd")} AA";
            //  命令 = "06 2E F1 8B 24 07 26 00";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    //  信息 = "解密失败";
                    return false;
                }
                bool result1, result2;

                foreach (var item in list1)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
                    if (!result1)
                    {
                        信息 = "发送数据失败:" + item;
                        return false;
                    }
                    Thread.Sleep(50);
                }

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
                响应 = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 32"))
                        {
                            信息 = "连接WIFI成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "连接WIFI失败";
                return false;

            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
            return true;
#endif
        }


        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 连接WIFI4C32(int 通道, out string 响应, out string 命令, out string 信息, string 账号 = "mFi_160868",
            string 密码 = "1234567890")
        {
            List<string> list = new List<string>()
            {
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
            };

            list.InsertRange(0, 账号.ToHexArray());
            list.InsertRange(账号.Length + 1, 密码.ToHexArray());
            信息 = "";
            响应 = "";
            List<string> list1 = new List<string>();
            string str10 = $"10 23 2E 4C 32 " + String.Join(" ", list.GetRange(0, 3)).Trim();
            string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
            string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
            string str23 = $"23 " + String.Join(" ", list.GetRange(17, 7)).Trim();
            string str24 = $"24 " + String.Join(" ", list.GetRange(24, 7)).Trim();
            string str25 = $"25 " + String.Join(" ", list.GetRange(31, 7)).Trim();
            list1.Add(str10);
            list1.Add(str21);
            list1.Add(str22);
            list1.Add(str23);
            list1.Add(str24);
            list1.Add(str25);
            命令 = string.Join("\n", list1);
            /*
                10 23 2E 4C 32 33 36 30
                21 57 69 46 69 00 77 65
                22 6E 79 6F 6E 67 31 32
                23 33 00 20 20 20 20 20
                24 20 20 20 20 20 20 20
                25 20 00 00 00 00 00 00
             */
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1;

                foreach (var item in list1)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
                    if (!result1)
                    {
                        信息 = "发送数据失败:" + item;
                        return false;
                    }
                    Thread.Sleep(50);
                }

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                响应 = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 32"))
                        {
                            信息 = "连接WIFI成功";
                            return true;
                        }
                    }
                }

                信息 = "连接WIFI失败";
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
            }
            return false;
        }
        //[ExposedMethodAttribute("")]
        //public bool 打开WIFI4C83(int 通道, out string 信息, out string data)
        //{
        //    data = "";
        //    try
        //    {
        //        if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
        //        {
        //            信息 = "启动can设备失败";
        //            return false;
        //        }
        //        _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
        //        bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
        //        if (!OK)
        //        {
        //            return false;
        //        }
        //        /*
        //           05 2E 4C 83 01 00 00 00
        //        */
        //        bool result1 = false;//05 2E 4C 83 01 00 00 00
        //        for (int i = 0; i < 3; i++)
        //        {
        //            result1 = zLGCANHelper.SentData(通道, 0x761, "05 2E 4C 83 01 00 00 00", true, SendType.正常发送);
        //            if (result1)
        //            {
        //                break;
        //            }
        //        }

        //        if (!result1)
        //        {
        //            data = "发送数据失败,打开WIFI失败";
        //            return false;
        //        }
        //        List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
        //        string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
        //        data = string.Join("\n", datas);
        //        if (datas.Length > 0)
        //        {
        //            foreach (string s in datas)
        //            {

        //                if (s.Trim().StartsWith("03 6E 4C 30"))
        //                {
        //                    信息 = "打开WIFI成功";
        //                    return true;
        //                }
        //            }

        //        }

        //        信息 = "打开WIFI失败";
        //    }
        //    catch (Exception ex)
        //    {
        //        信息 = "打开WIFI失败" + ex.Message;
        //    }
        //    return false;
        //}
        //[ExposedMethodAttribute("")]
        //public bool 关闭WIFI4C83(int 通道, out string 信息, out string data)
        //{
        //    data = "";
        //    try
        //    {
        //        if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
        //        {
        //            信息 = "启动can设备失败";
        //            return false;
        //        }
        //        _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
        //        bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
        //        if (!OK)
        //        {
        //            return false;
        //        }
        //        /*
        //           04 2E 4C 30 01 00 00 00
        //        */
        //        bool result1 = false;//05 2E 4C 83 01 00 00 00
        //        for (int i = 0; i < 3; i++)
        //        {
        //            result1 = zLGCANHelper.SentData(通道, 0x761, "05 2E 4C 83 02 00 00 00", true, SendType.正常发送);
        //            if (result1)
        //            {
        //                break;
        //            }
        //        }

        //        if (!result1)
        //        {
        //            data = "发送数据失败,关闭WIFI失败";
        //            return false;
        //        }
        //        List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
        //        string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);//03 6E 4C 83 AA AA AA AA 
        //        data = string.Join("\n", datas);
        //        if (datas.Length > 0)
        //        {
        //            foreach (string s in datas)
        //            {
        //                if (s.Trim().StartsWith("03 6E 4C 30"))
        //                {
        //                    信息 = "关闭WIFI成功";
        //                    return true;
        //                }
        //            }
        //        }
        //        信息 = "关闭WIFI失败";
        //    }
        //    catch (Exception ex)
        //    {
        //        信息 = "关闭WIFI失败" + ex.Message;
        //    }
        //    return false;
        //}
        //[ExposedMethodAttribute("")]
        //public bool 设置热点密码4C39(int 通道, out string 响应, out string 命令, out string 信息,
        //    string 密码 = "12345678")
        //{
        //    List<string> list = new List<string>()
        //    {
        //        "20", "20", "20","20", "20", "20","20", "20", "20",
        //        "20", "20", "20","20", "20", "20","20", "20", "20",
        //        "20", "20", "20","20", "20", "20","20", "20", "20",
        //        "20", "20", "20","20", "20", "20","20", "20", "20",
        //    };

        //    list.InsertRange(0, 密码.ToHexArray());
        //    //list.InsertRange(名称.Length + 1, 密码.ToHexArray());
        //    信息 = "";
        //    响应 = "";
        //    List<string> list1 = new List<string>();
        //    string str10 = $"10 13 2E 4C 39 " + String.Join(" ", list.GetRange(0, 3)).Trim();
        //    string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
        //    string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
        //    string str23 = $"23 " + String.Join(" ", list.GetRange(10, 7)).Trim();
        //    string str24 = $"24 " + String.Join(" ", list.GetRange(10, 7)).Trim();
           
        //    list1.Add(str10);
        //    list1.Add(str21);
        //    list1.Add(str22);
        //    //list1.Add(str23);
        //    //list1.Add(str24);
        //    命令 = string.Join("\n", list1);
        //    /*
        //        10 24 2E 4C 39 31 32 33  
        //        21 34 35 36 37 38 39 00
        //        22 00 00 00 00 00 00 00
        //        23 00 00 00 00 00 00 00
        //        24 00 00 00 00 00 00 00
        //        25 00 00 00 00 00 00 00
        //     */
        //    try
        //    {
        //        if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
        //        {
        //            信息 = "启动can设备失败";
        //            return false;
        //        }
        //        _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
        //        bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
        //        if (!OK)
        //        {
        //            信息 = "解密失败";
        //            return false;
        //        }
        //        bool result1;

        //        foreach (var item in list1)
        //        {
        //            result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
        //            if (!result1)
        //            {
        //                信息 = "发送数据失败:" + item;
        //                return false;
        //            }
        //            Thread.Sleep(50);
        //        }

        //        List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
        //        string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
        //        响应 = string.Join("\n", datas);
        //        if (datas.Length > 0)
        //        {
        //            foreach (string s in datas)
        //            {
        //                if (s.Trim().StartsWith("03 6E 4C 39"))
        //                {
        //                    信息 = "设置热点密码成功";
        //                    return true;
        //                }
        //            }
        //        }

        //        信息 = "设置热点密码失败";
        //    }
        //    catch (Exception ex)
        //    {
        //        信息 = ex.Message;
        //    }
        //    return false;
        //}
        [ExposedMethodAttribute("")]
        public bool 设置热点名称4C3A(int 通道, out string 响应, out string 命令, out string 信息, string 名称 = "mFi_Test"
           )
        {
            List<string> list = new List<string>()
            {
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
            };

            list.InsertRange(0, 名称.ToHexArray());
            //list.InsertRange(名称.Length + 1, 密码.ToHexArray());
            信息 = "";
            响应 = "";
            List<string> list1 = new List<string>();
            string str10 = $"10 13 2E 4C 3A " + String.Join(" ", list.GetRange(0, 3)).Trim();
            string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
            string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
            list1.Add(str10);
            list1.Add(str21);
            list1.Add(str22);
            命令 = string.Join("\n", list1);
            /*
                10 13 2E 4C 3A 31 32 33          
                21 34 35 36 37 38 39 00     
                22 00 00 00 00 00 00 00
             */
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1;

                foreach (var item in list1)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
                    if (!result1)
                    {
                        信息 = "发送数据失败:" + item;
                        return false;
                    }
                    Thread.Sleep(50);
                }

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                响应 = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 3A"))
                        {
                            信息 = "设置热点名称成功";
                            return true;
                        }
                    }
                }

                信息 = "设置热点名称失败";
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 设置热点名称4C39(int 通道, out string 响应, out string 命令, out string 信息, string 密码 = "12345678"
         )
        {
            List<string> list = new List<string>()
            {
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
                "20", "20", "20","20", "20", "20","20", "20", "20",
            };

            list.InsertRange(0, 密码.ToHexArray());
            //list.InsertRange(名称.Length + 1, 密码.ToHexArray());
            信息 = "";
            响应 = "";
            List<string> list1 = new List<string>();
            string str10 = $"10 13 2E 4C 39 " + String.Join(" ", list.GetRange(0, 3)).Trim();
            string str21 = $"21 " + String.Join(" ", list.GetRange(3, 7)).Trim();
            string str22 = $"22 " + String.Join(" ", list.GetRange(10, 7)).Trim();
            list1.Add(str10);
            list1.Add(str21);
            list1.Add(str22);
            命令 = string.Join("\n", list1);
            /*
                10 13 2E 4C 3A 31 32 33          
                21 34 35 36 37 38 39 00     
                22 00 00 00 00 00 00 00
             */
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1;

                foreach (var item in list1)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, item, true, SendType.正常发送);
                    if (!result1)
                    {
                        信息 = "发送数据失败:" + item;
                        return false;
                    }
                    Thread.Sleep(50);
                }

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                响应 = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 39"))
                        {
                            信息 = "设置热点密码成功";
                            return true;
                        }
                    }
                }

                信息 = "设置热点密码失败";
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 打开热点4C37(int 通道, out string 信息, out string data)
        {
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    return false;
                }
                /*
                   04 2E 4C 37 02 00 00 00
                */
                bool result1 = false;
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 37 02 00 00 00", true, SendType.正常发送);
                    if (result1)
                    {
                        break;
                    }
                }

                if (!result1)
                {
                    data = "发送数据失败,WIFI热点失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 1000);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {

                        if (s.Trim().StartsWith("03 6E 4C 37"))
                        {
                            信息 = "WIFI热点成功";
                            return true;
                        }
                    }
                }

                信息 = "WIFI热点失败";
            }
            catch (Exception ex)
            {
                信息 = "WIFI热点失败" + ex.Message;
            }
            return false;
        }
    }
}
